
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_aygsh/base/Constant.dart';
import 'subcate_page.dart';
import 'band_page.dart';
import '../liz_home/search/SearchGoods.dart';
import '../base/Constant.dart';
import 'package:flutter/services.dart';

class CatePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return CateFragmentState();
  }
}

class CateFragmentState extends State<CatePage> with WidgetsBindingObserver {
  TabController _tabController;
  // List tabs = ['分类', '品牌', '地区']; //顶级分类数组
  List tabs = ['分类', '品牌'];
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      if (!ModalRoute.of(context).isCurrent) {
        return;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return new DefaultTabController(
        length: tabs.length,
        child: Scaffold(
          appBar: AppBar(
            title: _buildSearch(),
            brightness: Brightness.light,
            backgroundColor: Colors.white,
            elevation: 0,
            bottom: TabBar(
              labelPadding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(DP_94), 0, ScreenUtil.getInstance().setWidth(DP_94), 0),
              indicatorPadding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(DP_94), 0, ScreenUtil.getInstance().setWidth(DP_94), 0),
              indicatorSize:TabBarIndicatorSize.tab,
              labelStyle: TextStyle(
                fontSize: ScreenUtil.getInstance()
                                                .setSp(NORMAL_FONT_SIZE)
              ),
              indicatorWeight: 2,
              indicatorColor: mainColor,
              labelColor: mainColor,
              unselectedLabelColor: Colors.black,
              isScrollable: true,
              tabs: tabs.map((item) {
                return _tabsBuilder(context, item);
              }).toList(),
              controller: _tabController,
              onTap: (index) {
                print(tabs[index]);
              },
            ),
          ),
          body: 
          AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.dark,
        child:TabBarView(
              children: <Widget>[SubCate(), BandCate()]),
              // , DistanceCate()
          //     Expanded(
          //       child:
          //       Row(
          //       mainAxisAlignment: MainAxisAlignment.start,
          //       children: <Widget>[
          //         Expanded(
          //           flex: 2,
          //           child: Container(
          //             color: divideColor,
          //             child: ListView.builder(
          //               itemCount: _datas.length,
          //               itemBuilder: (BuildContext context, int position) {
          //                 return getRow(position);
          //               },
          //             ),
          //           ),
          //         ),
          //         Expanded(
          //             flex: 5,
          //             child: Container(
          //               //height: double.infinity,
          //               alignment: Alignment.topLeft,
          //               padding: const EdgeInsets.all(10),
          //               color: Colors.white,
          //               child: getChip(index), //传入一级分类下标
          //             )),
          //       ],
          //     ),
          //     )
          //   ],
          // ),
        )));
  }

//搜索组件
  _buildSearch() {
    return Container(
      padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_15)),
      color: Colors.white,
      child: Center(
        child: new Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: new GestureDetector(
                child: Container(
                  padding: EdgeInsets.fromLTRB(
                      ScreenUtil.getInstance().setHeight(DP_10),
                      ScreenUtil.getInstance().setWidth(DP_10),
                      ScreenUtil.getInstance().setHeight(DP_10),
                      ScreenUtil.getInstance().setWidth(DP_10)),
                  margin: EdgeInsets.fromLTRB(
                      ScreenUtil.getInstance().setHeight(DP_10),
                      0,
                      ScreenUtil.getInstance().setHeight(DP_10),
                      0),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(245, 246, 247, 1),
                      borderRadius: BorderRadius.circular(20.0)),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Icon(
                        Icons.search,
                        size: ScreenUtil.getInstance().setWidth(DP_50),
                        color: Colors.black26,
                      ),
                      Text(
                        '请输入关键词搜索',
                        style: TextStyle(
                            color: Colors.black38,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(SMALL_FONT_SIZE)),
                      )
                    ],
                  ),
                ),
                onTap: () {
                           Navigator.of(context)
                                .push(new MaterialPageRoute(builder: (context) {
                              return new SearchGoodsPage();
                            }));
                          }
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _tabsBuilder(BuildContext context, item) {
    return Tab(text: item);
  }

  // Widget getRow(int i) {
  //   Color textColor = Theme.of(context).primaryColor; //字体颜色
  //   return GestureDetector(
  //     child: Container(
  //       padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
  //       color: index == i ? Colors.white : divideColor,
  //       //Container下的color属性会与decoration下的border属性冲突，所以要用decoration下的color属性
  //       child: Container(
  //         width: double.infinity,
  //         alignment: Alignment.center,
  //         padding: EdgeInsets.fromLTRB(
  //             ScreenUtil.getInstance().setHeight(DP_20),
  //             0.0,
  //             ScreenUtil.getInstance().setHeight(DP_20),
  //             00.0),
  //         decoration: BoxDecoration(
  //           color: index == i ? Colors.white : divideColor,
  //           border: Border(
  //             left: BorderSide(
  //                 width: 3, color: index == i ? mainColor : divideColor),
  //           ),
  //         ),
  //         child: Text(
  //           _datas[i].name,
  //           style: TextStyle(
  //             color: index == i ? mainColor : Colors.black54,
  //             fontWeight: index == i ? FontWeight.w600 : FontWeight.w400,
  //             fontSize: index == i
  //                 ? ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE)
  //                 : ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
  //           ),
  //         ),
  //       ),
  //     ),
  //     onTap: () {
  //       setState(() {
  //         index = i; //记录选中的下标
  //         textColor = mainColor;
  //       });
  //     },
  //   );
  // }

//   Widget getChip(int i) {
//     //更新对应下标数据
//     _updateArticles(i);
//     return GridView.builder(
//         itemCount: articles.length,
//         //SliverGridDelegateWithFixedCrossAxisCount 构建一个横轴固定数量Widget
//         gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
//             //横轴元素个数
//             crossAxisCount: 3,
//             //纵轴间距
// //            //横轴间距
// //            crossAxisSpacing: 10.0,
//             childAspectRatio: 0.8),
//         itemBuilder: (BuildContext context, int index) {
//           //Widget Function(BuildContext context, int index)
//           return getItemContainer(articles[index]);
//         });
//   }

  // Widget getItemContainer(NativeDataArticle item) {
  //   return new SingleChildScrollView(
  //     child: new GestureDetector(
  //       onTap: () {
  //         // Navigator.of(context).push(new MaterialPageRoute(builder:(context){
  //         //     return new ServiceListByCatPage(cat_id: item.cat_id,cat_name: item.name,type:2);
  //         // }));
  //       },
  //       child: new ConstrainedBox(
  //           constraints: new BoxConstraints(minHeight: 1),
  //           child: Column(
  //             children: <Widget>[
  //               Container(
  //                   padding: EdgeInsets.all(5),
  //                   alignment: Alignment.center,
  //                   child: new ClipOval(
  //                       child: Image.network(
  //                     Api.BaseUrl + item.icon,
  //                     width: ScreenUtil.getInstance().setWidth(DP_100),
  //                     height: ScreenUtil.getInstance().setWidth(DP_100),
  //                     fit: BoxFit.fill,
  //                   ))),
  //               Text(
  //                 item.name,
  //                 style: TextStyle(
  //                     fontSize: ScreenUtil.getInstance().setSp(MINI_FONT_SIZE)),
  //                 textAlign: TextAlign.center,
  //                 maxLines: 1,
  //                 overflow: TextOverflow.ellipsis,
  //               ),
  //             ],
  //           )),
  //     ),
  //   );
  // }

  // /// 根据一级分类下标更新二级分类集合
  // List<NativeDataArticle> _updateArticles(int i) {
  //   setState(() {
  //     if (_datas.length != 0) articles = _datas[i].articles;
  //   });
  //   return articles;
  // }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  void getHttp() async {
    // try {
    //   var response = await HttpUtil().get(Api.GET_ALL_SERVICE_CAT);
    //   Map userMap = json.decode(response.toString());
    //   var naviEntity = NativeEntity.fromJson(userMap);
    //   setState(() {
    //     _datas = naviEntity.data;
    //     index = 0;
    //   });
    // } catch (e) {
    //   print(e);
    // }
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this); //销毁观察者
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}
